﻿using UnityEngine;
using System.Collections;

namespace Lost{
	/// <summary>
	/// 控制蜡烛虚像的位置
	/// </summary>
	public class CandleCtrl : MonoBehaviour {
		[SerializeField] Transform candle;
		[SerializeField] Transform glass;
		[SerializeField] ParticleSystem candleParSys;
		[SerializeField] float focalDis = 3f;
		ParticleSystem virtualParSys;
		// Use this for initialization
		void Start () {
			virtualParSys = GetComponentInChildren<ParticleSystem> ();
		}

		// Update is called once per frame
		void Update () {
			Vector3 axis = glass.right;
			float realDis = Mathf.Abs (Vector3.Dot (candle.position - glass.position, axis));
			float virtualDis = focalDis * realDis / (realDis - focalDis);//1f / (1 / focalDis - 1 / realDis);
			float magnification = (virtualDis / focalDis - 1);
			transform.position = glass.position + (glass.position-candle.position).normalized*virtualDis;
			transform.localScale = candle.localScale * magnification;
			//virtualParSys.transform.localPosition = candleParSys.transform.localPosition * magnification;
			virtualParSys.startSize = candleParSys.startSize * magnification;
			virtualParSys.startSpeed = candleParSys.startSpeed * magnification; 

			//Vector3 direction = candle.forward;
			//float d = Vector3.Dot (direction, axis);
			//direction = d * 2f * axis - direction;
			//transform.LookAt (transform.position + direction);
			transform.LookAt (transform.position - candle.forward);
		}
	}
}